/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 */
package com.jeesite.modules;

import com.jeesite.modules.filter.FileUploadFilter;
import com.jeesite.modules.swagger.config.SwaggerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Application
 * @author ThinkGem
 * @version 2018-10-13
 */
@EnableAsync
@SpringBootApplication
@ServletComponentScan
public class Application extends SpringBootServletInitializer {
	
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		this.setRegisterErrorPageFilter(false); // 错误页面有容器来处理，而不是SpringBoot
		return builder.sources(Application.class);
	}
	@Bean
	public RestTemplate restTemplate(){
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		// 设置连接超时，单位毫秒
		requestFactory.setConnectTimeout(30000);
		//设置读取超时
		requestFactory.setReadTimeout(30000);
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.setRequestFactory(requestFactory);
		return restTemplate;
	}
	@Bean
	public FilterRegistrationBean filterRegistrationBean(FileUploadFilter fileUploadFilter) {
		FilterRegistrationBean registration = new FilterRegistrationBean();

		registration.setFilter(fileUploadFilter);
		//过滤器名称
		registration.setName("fileUploadFilter");
		//拦截路径
		registration.addUrlPatterns("/*");
		//设置顺序
		registration.setOrder(10);
		return registration;
	}
	@Bean
	@ConditionalOnProperty(
			name = {"web.swagger.enabled"},
			havingValue = "true",
			matchIfMissing = true
	)
	public Docket webApi() {
		String moduleCode = "103-web-home模块";
		String moduleName = "home模块";
		String basePackage = "com.jeesite.modules.home.web";
		return SwaggerConfig.docket(moduleCode, moduleName, basePackage).select().apis(RequestHandlerSelectors.any().and(RequestHandlerSelectors.basePackage(basePackage))).build();
	}
}